package itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import itheima.Mapper.CategoryMapper;
import itheima.common.CustomExcrption;
import itheima.entity.Category;
import itheima.entity.Dish;
import itheima.entity.Setmeal;
import itheima.service.CategorySerevice;
import itheima.service.DishService;
import itheima.service.EmployeeService;
import itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shkstart
 * @create 2022-09-16-16:38
 */
@Service
public class CategoryServiceimpl extends ServiceImpl<CategoryMapper, Category> implements CategorySerevice {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;
    /**
     *  根据id删除看分类，删除 之前需要进行判断
     * @param id
     */

    @Override
    public void remove(Long id) {

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);//等值查询id
        int count1 = dishService.count(dishLambdaQueryWrapper);//得到数量赋值给count1

        //查询当前分类是否关联了菜品，如果已经关联，抛出一个业务异常

        if (count1 > 0){
            //已经关联菜品，抛出一个业务异常
            throw new CustomExcrption("当前分类下关联了菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);

        //查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常

        if (count2 > 0){
            //已经关联套餐，抛出一个业务异常
            throw new CustomExcrption("当前分类下关联了套餐，不能删除");

        }
        //正常删除分类
        super.removeById(id);
    }
}
